package ru.ifmo.math.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class Cycled {

    Map<Integer, Integer> spectrum = new HashMap<Integer, Integer>();

    public void recursive(Node node, int w) {
        if (node.isZeroWay) {
            Integer prev = spectrum.get(w);
            if (prev == null) {
                prev = 0;
            }
            prev++;
            spectrum.put(w, prev);
            return;
        }
        if (w < 14) {
            for (Reference ref : node.next) {
                recursive(ref.ref, w + ref.weight);
            }
        }
    }

    public void run() {
        Node node00 = new Node();
        Node node01 = new Node();
        Node node10 = new Node();
        Node node11 = new Node();

        node00.isZeroWay = true;

        node00.next.add(new Reference(node00, 0));
        node00.next.add(new Reference(node10, 1));

        node10.next.add(new Reference(node01, 3));
        node10.next.add(new Reference(node11, 2));

        node01.next.add(new Reference(node10, 1));
        node01.next.add(new Reference(node00, 2));

        node11.next.add(new Reference(node01, 1));
        node11.next.add(new Reference(node11, 2));

        recursive(node10, 1);

        for (int i = 0; i < 12; i++) {
            Integer res = spectrum.get(i);
            if (res != null) {
                System.out.println(i + " -> " + res);
            }
        }
    }

    public static void main(String[] args) {
        new Cycled().run();
    }

    class Reference {
        public Reference(Node ref, int weight) {
            this.ref = ref;
            this.weight = weight;
        }

        Node ref;
        int weight;
    }

    class Node {
        List<Reference> next = new ArrayList<Reference>();
        boolean isZeroWay = false;
    }
}
